package cn.tedu.egnotice01.controller;

import cn.tedu.egnotice01.mapper.NoticeMapper;
import cn.tedu.egnotice01.pojo.datatransfer.NoticeAddParam;
import cn.tedu.egnotice01.pojo.datatransfer.NoticeListQuery;
import cn.tedu.egnotice01.pojo.datatransfer.NoticeUpdateParam;
import cn.tedu.egnotice01.pojo.entity.Notice;
import cn.tedu.egnotice01.pojo.valueobject.NoticeDetailInfo;
import cn.tedu.egnotice01.pojo.valueobject.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String noticeAddMessage(NoticeAddParam noticeAddParam) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        return "添加成功";
    }

    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> queryNoticeMessage(NoticeListQuery noticeListQuery) {
        List<NoticeListVO> noticeListVOList = noticeMapper.queryNotice(noticeListQuery);
        return noticeListVOList;
    }
    @GetMapping("/v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfo queryDetailNoticeMessage(Long id){
        NoticeDetailInfo noticeDetailInfo = noticeMapper.queryDetailNotice(id);
        return noticeDetailInfo;
    }
    @PostMapping("/v1/notice/del")
    @ResponseBody
    public String deleteNoticeMessage(Long id){
        noticeMapper.deleteNotice(id);
        return "删除成功";
    }
    @PostMapping("/v1/notice/update")
    @ResponseBody
    public String noticeUpdate(NoticeUpdateParam noticeUpdateParam) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        int row = noticeMapper.updateNotice(notice);
        if (row>0){
            return "更新资讯成功";
        }else{
            return "更新资讯失败";
        }
    }
}
